<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分类名称" prop="catName">
        <el-input v-model="queryParams.catName" placeholder="请输入名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['jky:goods-cat:create']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['jky:goods-cat:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list" row-key="catId" :default-expand-all="isExpandAll"
              :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="供应商" align="center" prop="supplierName"/>
      <el-table-column label="分类名称" align="center" prop="catName"/>
      <el-table-column label="分类图标" align="center" prop="img">
        <template v-slot="scope">
          <span><img :src="scope.row.img" alt="图片不存在"></span>
        </template>
      </el-table-column>
      <el-table-column label="上架/下架" align="center" prop="isShow">
        <template v-slot="scope">
            <el-button size="small" type="danger" v-if="scope.row.isShow === 'Y'" @click="isShowHandle(scope.row,'N')">下架</el-button>
            <el-button size="small" type="success" v-else @click="isShowHandle(scope.row,'Y')">上架</el-button>
        </template>
      </el-table-column>
      <el-table-column label="是否推荐" align="center" prop="isTop">
        <template v-slot="scope">
          <el-button size="small" type="success" v-if="scope.row.isTop === 'N'" @click="isTopHandle(scope.row,'Y')">置顶</el-button>
          <el-button size="small" type="danger" v-else @click="isTopHandle(scope.row,'N')">不置顶</el-button>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort">
        <template v-slot="scope">
          <el-input style="width: 50px" @keydown.enter.native="sortHandle(scope.row)" v-model="scope.row.sort"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['jky:goods-cat:update']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['jky:goods-cat:delete']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="900px" v-dialogDrag append-to-body>
      <el-card class="form-container" shadow="never">
        <el-steps :active="active" finish-status="success" align-center>
          <el-step title="商品分类基本信息"></el-step>
          <el-step title="商品分类详情"></el-step>
        </el-steps>
        <cat-info-detail ref="info" v-model="form" :is-edit="isEdit" v-show="showStatus[0]" @nextStep="nextStep"
                         @cancel="cancel"/>
        <cat-content-detail v-model="form" :is-edit="isEdit" v-show="showStatus[1]" @prevStep="prevStep"
                            @cancel="cancel" @submit="submitForm"/>
      </el-card>
    </el-dialog>
  </div>
</template>

<script>
import {
  createGoodsCat,
  updateGoodsCat,
  deleteGoodsCat,
  getGoodsCat,
  getGoodsCatPage,
  exportGoodsCatExcel, updateGoodsCatState,
} from "@/api/supplier/goodsCat";

import CatContentDetail from "@/views/supplier/goodsCat/components/CatContentDetail";
import CatInfoDetail from "@/views/supplier/goodsCat/components/CatInfoDetail";
import step from '@/mixins/step'

export default {
  name: "GoodsCat",
  components: {
    CatInfoDetail,
    CatContentDetail
  },
  data() {
    return {
      // 是否展开
      isExpandAll: true,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品分类列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        catName: null,
        keywords: null,
        description: null,
        content: null,
        sort: null,
        isShow: null,
        img: null,
        parentId: 0,
        isTop: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        catName: [{required: true, message: "名称不能为空", trigger: "blur"}],
        isShow: [{required: true, message: "是否显示不能为空", trigger: "blur"}],
        parentId: [{required: true, message: "父ID不能为空", trigger: "blur"}],
        type: [{required: true, message: "类型不能为空", trigger: "change"}],
      },
      menuOptions: [],
      showStatus: [true, false],
      isEdit: false
    };
  },
  mixins: [step],
  created() {
    this.getList();
  },
  methods: {
    sortHandle(row) {
      console.log(row)
      updateGoodsCatState({catId: row.catId,sort: row.sort}).then(res => {
        this.$modal.msgSuccess('排序修改成功');
        this.open = false;
        this.getList();
      })
    },
    isShowHandle(row,state) {
      console.log('row===',row)
        updateGoodsCatState({catId: row.catId,isShow: state}).then(res => {
          this.$modal.msgSuccess(state === 'Y' ? "上架成功" : '下架成功');
          this.open = false;
          this.getList();
        })
    },
    isTopHandle(row,state) {
      updateGoodsCatState({catId: row.catId,isTop: state}).then(res => {
        this.$modal.msgSuccess(state === 'Y' ? "置顶成功" : '不置顶成功');
        this.open = false;
        this.getList();
      })
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getGoodsCatPage(this.queryParams).then(response => {
        this.list = this.handleTree(response.data.list, "catId");
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.active = 0
      this.hideAll()
      this.showStatus[0] = true
      this.form = {
        catName: undefined,
        keywords: undefined,
        description: undefined,
        content: undefined,
        sort: undefined,
        isShow: 'N',
        img: undefined,
        parentId: undefined,
        isTop: undefined,
        type: undefined,
      };
      this.resetForm("form");

    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品分类";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      const catId = row.catId;
      getGoodsCat(catId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      // 修改的提交
      if (this.form.catId != null) {
        updateGoodsCat(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;

          this.getList();
        });
        return;
      }
      // 添加的提交
      createGoodsCat(this.form).then(response => {
        this.$modal.msgSuccess("新增成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const catId = row.catId;
      this.$modal.confirm('是否确认删除商品分类编号为"' + catId + '"的数据项?').then(function () {
        return deleteGoodsCat(catId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有商品分类数据项?').then(() => {
        this.exportLoading = true;
        return exportGoodsCatExcel(params);
      }).then(response => {
        this.$download.excel(response, '商品分类.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    }
  }
};
</script>
